<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>植物识别与问答</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        #chat-box {
            max-width: 600px;
            margin: 20px auto;
            border: 1px solid #ccc;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 5px;
        }
        .message {
            margin: 10px 0;
        }
        .message.user {
            color: blue;
        }
        .message.ai {
            color: green;
        }
        #user-input {
            width: calc(100% - 110px);
            padding: 10px;
            margin-right: 10px;
        }
        #send-btn {
            padding: 10px;
            cursor: pointer;
        }
        #plant-upload {
            margin-top: 30px;
            border: 1px solid #ccc;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 5px;
        }
        #image-preview {
            max-width: 100%;
            max-height: 300px;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div id="chat-box">
        <div id="messages">
            <!-- 这里显示聊天记录 -->
        </div>
        <input type="text" id="user-input" placeholder="请输入植物问题" />
        <button id="send-btn">提问</button>
    </div>

    <div id="plant-upload">
        <h3>上传植物图片</h3>
        <input type="file" id="file-input" />
        <button id="upload-btn">上传并识别</button>
        <div id="uploaded-image-container">
            <!-- 这里用来显示上传的图片预览 -->
            <img id="image-preview" class="image-preview" src="" alt="上传的植物图片预览" />
        </div>
        <div id="plant-name"></div>
    </div>

    <script>
        const userInput = document.getElementById('user-input');
        const sendBtn = document.getElementById('send-btn');
        const messagesDiv = document.getElementById('messages');
        const fileInput = document.getElementById('file-input');
        const uploadBtn = document.getElementById('upload-btn');
        const plantNameDiv = document.getElementById('plant-name');
        const imagePreview = document.getElementById('image-preview');

        // 发送问题
        sendBtn.addEventListener('click', async () => {
            const query = userInput.value.trim();
            if (!query) return;

            // 显示用户问题
            updateChat(query, 'user');
            userInput.value = '';  // 清空输入框

            // 发送请求到后端
            const response = await fetch('/ask', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ question: query })
            });

            const data = await response.json();
            updateChat(data.answer, 'ai');  // 显示AI的回答
        });

        // 更新聊天记录
        function updateChat(message, role) {
            const messageDiv = document.createElement('div');
            messageDiv.classList.add('message', role);
            messageDiv.textContent = message;
            messagesDiv.appendChild(messageDiv);
            messagesDiv.scrollTop = messagesDiv.scrollHeight;  // 自动滚动到最新消息
        }

        // 处理图片上传并识别植物
        uploadBtn.addEventListener('click', async () => {
            const file = fileInput.files[0];
            if (!file) return;

            const formData = new FormData();
            formData.append('image', file);

            // 显示上传的图片预览
            const reader = new FileReader();
            reader.onload = function(e) {
                imagePreview.src = e.target.result;
                imagePreview.style.display = 'block';  // 显示图片
            };
            reader.readAsDataURL(file);

            const response = await fetch('/upload', {
                method: 'POST',
                body: formData
            });

            const data = await response.json();
            plantNameDiv.textContent = `识别结果：${data.plant_name}`;
        });
    </script>
</body>
</html>
